package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.shop.domain.SysBizUserAddress;
import com.member.shop.domain.SysBizUserSign;
import com.member.shop.mapper.SysBizUserAddressMapper;
import com.member.shop.mapper.SysBizUserSignMapper;
import com.member.shop.service.ISysBizUserSignService;
import com.member.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 会员签到Service业务层处理
 *
 * @author thomas
 * @date 2024-05-18
 */
@Service
public class SysBizUserSignServiceImpl extends ServiceImpl<SysBizUserSignMapper, SysBizUserSign> implements ISysBizUserSignService {
    @Resource
    private SysBizUserSignMapper sysBizUserSignMapper;

    /**
     * 查询会员签到
     *
     * @param id 会员签到主键
     * @return 会员签到
     */
    @Override
    public SysBizUserSign selectSysBizUserSignById(Long id) {
        return sysBizUserSignMapper.selectSysBizUserSignById(id);
    }

    /**
     * 查询会员签到列表
     *
     * @param sysBizUserSign 会员签到
     * @return 会员签到
     */
    @Override
    public List<SysBizUserSign> selectSysBizUserSignList(SysBizUserSign sysBizUserSign) {
        return sysBizUserSignMapper.selectSysBizUserSignList(sysBizUserSign);
    }

    @Override
    public List<SysBizUserSign> selectSysBizUserSignList(LambdaQueryWrapper<SysBizUserSign> queryWrapper) {
        return this.sysBizUserSignMapper.selectList(queryWrapper);
    }

    @Override
    public SysBizUserSign selectSysBizUserSignByDate(Long userId, LocalDate signDate) {

        SysBizUserSign sysBizUserSign = new SysBizUserSign();
        sysBizUserSign.setSignDate(signDate);
        sysBizUserSign.setUserId(userId);
        List<SysBizUserSign> sysBizUserSigns = sysBizUserSignMapper.selectSysBizUserSignList(sysBizUserSign);
        if (CollectionUtils.isEmpty(sysBizUserSigns)) {
            return null;

        }
        return sysBizUserSigns.get(0);
    }

    @Override
    public SysBizUserSign selectLatestSysBizUserSignByDate(Long userId, LocalDate signDate) {

        SysBizUserSign sysBizUserSign = selectSysBizUserSignByDate(userId, signDate);
        if (sysBizUserSign != null) {
            return sysBizUserSign;
        }
        signDate = signDate.minusDays(1);
        sysBizUserSign = selectSysBizUserSignByDate(userId, signDate);
        return sysBizUserSign;
    }

    /**
     * 新增会员签到
     *
     * @param sysBizUserSign 会员签到
     * @return 结果
     */
    @Override
    public int insertSysBizUserSign(SysBizUserSign sysBizUserSign) {
        sysBizUserSign.setCreateTime(DateUtils.getNowDate());
        return sysBizUserSignMapper.insertSysBizUserSign(sysBizUserSign);
    }

    /**
     * 修改会员签到
     *
     * @param sysBizUserSign 会员签到
     * @return 结果
     */
    @Override
    public int updateSysBizUserSign(SysBizUserSign sysBizUserSign) {
        sysBizUserSign.setUpdateTime(DateUtils.getNowDate());
        return sysBizUserSignMapper.updateSysBizUserSign(sysBizUserSign);
    }

    /**
     * 批量删除会员签到
     *
     * @param ids 需要删除的会员签到主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserSignByIds(Long[] ids) {
        return sysBizUserSignMapper.deleteSysBizUserSignByIds(ids);
    }

    /**
     * 删除会员签到信息
     *
     * @param id 会员签到主键
     * @return 结果
     */
    @Override
    public int deleteSysBizUserSignById(Long id) {
        return sysBizUserSignMapper.deleteSysBizUserSignById(id);
    }
}
